package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.AwardRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.AwardMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.Award;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IAwardService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 奖项业务层处理
 *
 * @author mars
 * @date 2024-04-27
 */
@Slf4j
@Service
@AllArgsConstructor
public class AwardServiceImpl implements IAwardService {

    private final AwardMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Award add(AwardRequest request) {
        Award entity = Award.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AwardRequest request) {
        Award entity = Award.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public Award getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<Award> pageList(AwardRequest request) {
        Page<Award> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<Award> query = this.buildWrapper(request);
        IPage<Award> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<Award> list(AwardRequest request) {
        LambdaQueryWrapper<Award> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<Award> buildWrapper(AwardRequest param) {
        LambdaQueryWrapper<Award> query = new LambdaQueryWrapper<>();
        if (param.getActivityId() != null){
               query.eq(Award::getActivityId ,param.getActivityId());
        }
         if (StringUtils.isNotBlank(param.getName())){
               query.like(Award::getName ,param.getName());
        }
        if (param.getCount() != null){
               query.eq(Award::getCount ,param.getCount());
        }
         if (StringUtils.isNotBlank(param.getAward())){
               query.like(Award::getAward ,param.getAward());
        }
        return query;
    }

}
